import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import postcsspxtoviewport from "postcss-px-to-viewport" 
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
// import { VantResolver } from '@vant/auto-import-resolver';
import { VantResolver } from 'unplugin-vue-components/resolvers'
const ENVURL = process.env.VITE_API_BASE_URL
// const designWidth = file.dirname.includes(path.join('node_modules', 'vant')) ? 375 : 750;
const loder_pxtovw = postcsspxtoviewport({
  unitToConvert: 'px', // 要转化的单位
  viewportWidth: 1179,
  unitPrecision: 6, // 转换后的精度，即小数点位数
  propList: ['*'], // 指定转换的css属性的单位，*代表全部css属性的单位都进行转换
  viewportUnit: 'vw', // 指定需要转换成的视窗单位，默认vw
  fontViewportUnit: 'vw', // 指定字体需要转换成的视窗单位，默认vw
  selectorBlackList: ['ignore-'], // 指定不转换为视窗单位的类名，
  minPixelValue: 1, // 默认值1，小于或等于1px则不进行转换
  mediaQuery: true, // 是否在媒体查询的css代码中也进行转换，默认false
  replace: false, // 是否转换后直接更换属性值
  landscape: false,// 是否处理横屏情况
  exclude:[/node_modules\/vant/i]
})
const vant_pxtovw = postcsspxtoviewport({
  unitToConvert: 'px', // 要转化的单位
  viewportWidth: 375, // UI设计稿的宽度
  unitPrecision: 6, // 转换后的精度，即小数点位数
  propList: ['*'], // 指定转换的css属性的单位，*代表全部css属性的单位都进行转换
  viewportUnit: 'vw', // 指定需要转换成的视窗单位，默认vw
  fontViewportUnit: 'vw', // 指定字体需要转换成的视窗单位，默认vw
  selectorBlackList: ['ignore-'], // 指定不转换为视窗单位的类名，
  minPixelValue: 1, // 默认值1，小于或等于1px则不进行转换
  mediaQuery: true, // 是否在媒体查询的css代码中也进行转换，默认false
  replace: false, // 是否转换后直接更换属性值
  landscape: false,// 是否处理横屏情况
  exclude: [/^(?!.*node_modules\/vant)/] //忽略除vant之外的
})
console.log('ENVURL',ENVURL)
// https://vite.dev/config/
export default defineConfig({
  resolve: {
    alias: {
      '@': resolve(__dirname, './src'),
      '#': resolve(__dirname, './src/types'),
      utils: resolve(__dirname, './src/utils'),
      api: resolve(__dirname, './src/api')
    }
  },
  plugins: [
    vue(),
    AutoImport({
      resolvers: [VantResolver()],
    }),
    Components({
      resolvers: [VantResolver()],
    }),
  ],
  //设置反向代理，跨域
  server: {
    // host: '0.0.0.0',//设置地址：http://localhost
    port: 8080, // 设置服务启动端口号
    // open: true, // 设置服务启动时是否自动打开浏览器
    // cors: true, // 允许跨域
    proxy: {
      '/xysd-msg': {
        target: 'http://localhost:3001/',//代理的地址
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/xysd-msg/, '')//这里的/需要转义
      },
      "/xysd-xfxt": {
        // target: "http://192.168.0.245",
        // target: "http://192.168.0.120:8008", //wq服务地址
        target: "http://192.168.0.109", //首开信访
        // target: "http://192.168.0.65:8008", //dz服务地址
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/xysd-xfxt/, '')
      },
      "/xysd-sys": {
        // target: "http://192.168.0.245:8080",
        target: "http://192.168.0.109",
        // target: ENVURL,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/xysd-sys/, '')
      },
      "/xysd-portal": {
        // target: "http://192.168.0.245:8080",
        target: ENVURL,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/xysd-portal/, '')
      },
      "/xysd-filesys": {
        // target: "http://192.168.0.245:8080",
        target: "http://192.168.0.109",
        // target: ENVURL,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/xysd-filesys/, '')
      },
      "/xysd-stat": {
        // target: "http://192.168.0.245:8080",
        target: "http://192.168.0.109",
        // target: ENVURL,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/xysd-stat/, '')
      },
      "/xysd-assistoffice": {
        // target: "http://192.168.0.245:8080",
        target: ENVURL,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/xysd-assistoffice/, '')
      },
      "/baidumap": {
        target: "http://192.168.0.109", //测试环境地址
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/baidumap/, '')
      },
      "/yxhy-common-user": {
        target: "http://192.168.0.109", //测试环境地址
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/yxhy-common-user/, '')
      },
      "/skyht-platform-base": {
        target: "http://192.168.0.109", //测试环境地址
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/skyht-platform-base/, '')
      }
    }
  },

  css: {
    postcss: {
      plugins: [loder_pxtovw,vant_pxtovw],
    },
  },
})
